package com.example.demo.controller;

import com.example.demo.dao.LevelDao;
import com.example.demo.entity.LevelBean;
import com.example.demo.entity.UserBean;
import com.example.demo.service.LevelService;
import com.example.demo.service.UserService;
import com.example.demo.util.PageEntity;
import com.example.demo.util.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: demo
 * @ClassName LevelController
 * @description:
 * @author: xtf
 * @create: YEAR−{MONTH}-DAY{HOUR}:14
 * @Version 1.0
 **/



// @Api   @ApiOperation    @ApiImplicitParam

@RestController
@RequestMapping("/level")
@Api(tags = "会员等级管理",hidden = false)
public class LevelController {
    @Autowired
    private LevelService levelService;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @PostMapping("/add")
    public ResponseEntity add(@RequestBody LevelBean levelBean){

        Integer insert = levelService.add(levelBean);

        if(insert>0){
            return ResponseEntity.success("新增成功");
        }else{
            return ResponseEntity.failed("新增失败");
        }
    }


    @PostMapping("/edit")
    public ResponseEntity edit(@RequestBody LevelBean levelBean){
        Integer update = levelService.update(levelBean);
        if(update>0){
            return ResponseEntity.success("修改成功");
        }else{
            return ResponseEntity.failed("修改失败");
        }

    }

    @GetMapping("/del")
    public ResponseEntity del(@RequestParam("id") Integer id){
        Integer delete = levelService.delete(id);
        if(delete>0){
            return ResponseEntity.success("删除成功");
        }else{
            return ResponseEntity.failed("删除失败");
        }

    }


    @GetMapping("/get/{id}")
    public  ResponseEntity getOne(@PathVariable Integer id){

        LevelBean one = levelService.one(id);
        ResponseEntity re = ResponseEntity.success("获取成功");
        re.setData(one);

        return re;
    }

    /**
     * 分页
     * @param pageNo
     * @param pageSize
     * @param name
     * @param sort 排序字段
     * @param order  排序方式  desc、asc
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity list(
            @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "name",defaultValue = "") String name,
            @RequestParam(value = "sort",defaultValue = "id") String sort,
            @RequestParam(value = "order",defaultValue = "desc") String order
    ){

        // 延迟加载  按需加载  懒加载
        //  不能进行关联查询  分为多条sql：
        //   主语句  select　＊　　ｆｒｏｍ　ｕｓｅｒ
        //   从语句 select * from level  where id=?

        String  sortOrder = sort+" "+order;
        PageEntity list = levelService.list(pageNo, pageSize, name, sortOrder);
        ResponseEntity<PageEntity> data = new ResponseEntity<>(200,null,list);
        // 按需加载
        return data;
    }

//    @GetMapping("/all")
//    @ApiOperation("会员列表")
//    @ApiImplicitParam(name = "keywords",value = "搜索关键字：等级名称，等级变化",defaultValue = "e.g张三",required = true)
//    public List<LevelBean> get(String keywords){
//
//        return levelService.all();
//    }
//
//
//    @GetMapping("/test")
//    @ApiOperation("测试-忽略")
//    public String test(){
//        // 一级缓存：
//        //  1. 同一个sqlSession
//        //  2. 执行的sql语句一样
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//
//        LevelDao levelDao = sqlSession.getMapper(LevelDao.class);
//
//        LevelBean level1 = levelDao.getOneById(1);
//
//        SqlSession sqlSession2 = sqlSessionFactory.openSession();
//
//        LevelDao levelDao2 = sqlSession2.getMapper(LevelDao.class);
//
//        LevelBean level2 = levelDao2.getOneById(1);
//        // 第二条语句命中缓存
////        LevelBean level2 = levelDao.getOneById(1);
//        // 在执行 update delete insert  会自动的清除缓存
//
//        System.out.println(level1);
//        System.out.println(level2);
//
//
//
//        return "一级缓存";
//    }


}